package com.css.bjdt_core.dataDifference.service;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.nutz.dao.Condition;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.nutz.dao.sql.SqlCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.css.bjdt_core.reportFill.entity.CellDataValue;
import com.css.bjdt_core.util.ReportConstant;

@Service
public class DataDifferenceService {
	@Autowired
	private Dao dao;

	public Map getDataDifferenceMap(Condition cnd, int pageNum, int pageSize, String annualId, String dzzId) {
		String cndStr = cnd.toString();
		String tableName = ReportConstant.EXECUTIVE_TABLE_PREFIX + annualId;
		//Pager pager = dao.createPager(pageNum, pageSize);
		Sql strSql = Sqls
				.create("select t.CELL_DATA_ID,t.CELL_VALUE,t.ABS_ROW_NUM,t.ABS_COL_NUM,t.REL_ROW_NUM,t.REL_COL_NUM,t.DZZ_ID,t.TEMPLATE_ID,t.HISTORY_CELL_VALUE,to_char(t.INSERT_DATE,'yyyy-mm-dd hh24:mi:ss') as INSERT_DATE,rt.NAME from "
						+ tableName
						+ " t left join RPT_TEMPLATE rt on t.TEMPLATE_ID = rt.TEMPLATEID where t.DZZ_ID = @dzzId and SUM_STATUS is null and CELL_TYPE = '1' "
						+ cndStr.replace("WHERE", "AND"));
		//strSql.setPager(pager);
		strSql.params().set("dzzId", dzzId);
		//pager.setRecordCount();
		//int count = Daos.queryCount(dao, strSql.toString());
		strSql.setCallback(new SqlCallback() {
			@Override
			public Object invoke(Connection conn, ResultSet rs, Sql strSql) throws SQLException {
				List<CellDataValue> cellDataValueList = new ArrayList<CellDataValue>();
				while (rs.next()) {
					CellDataValue cellDataValue = new CellDataValue();
					cellDataValue.setAbsColNum(rs.getString("abs_col_num"));
					cellDataValue.setAbsRowNum(rs.getString("abs_row_num"));
					cellDataValue.setRelColNum(rs.getString("REL_COL_NUM"));
					cellDataValue.setRelRowNum(rs.getString("REL_ROW_NUM"));
					cellDataValue.setCellValue(rs.getString("CELL_VALUE"));
					cellDataValue.setCellDataId(rs.getString("CELL_DATA_ID"));
					cellDataValue.setHistoryCellValue(rs.getString("HISTORY_CELL_VALUE"));
					cellDataValue.setInsertDate(rs.getString("INSERT_DATE"));
					cellDataValue.setTemplateName(rs.getString("NAME"));
					cellDataValueList.add(cellDataValue);
				}
				return cellDataValueList;
			}
		});
		dao.execute(strSql);
		List<CellDataValue> cellDataValueList = strSql.getList(CellDataValue.class);
		List<CellDataValue> cellDataDifferenceList = new ArrayList<CellDataValue>();
		for (CellDataValue cellDataValue : cellDataValueList) {
			if (StringUtils.isNotEmpty(cellDataValue.getCellValue())) {
				//判断CELLVALUE和HistoryCellValue都不能为0 如果都为0 说明该指标压根就没填过  
				if (!cellDataValue.getCellValue().equals(cellDataValue.getHistoryCellValue())) {
					cellDataValue.setAbsCoordinate(
							"[" + cellDataValue.getAbsRowNum() + "_" + cellDataValue.getAbsColNum() + "]");
					cellDataDifferenceList.add(cellDataValue);
				}
			}
		}
		Map<String, Object> result = new HashMap<String, Object>();
		int startSize = (pageNum * pageSize) - pageSize;
		//int endSize = (pageNum * pageSize) - 1;
		int endSize = pageNum * pageSize;
		List<CellDataValue> newList = new ArrayList<CellDataValue>();
		if (cellDataDifferenceList.size() < endSize) {
			newList = cellDataDifferenceList.subList(startSize, cellDataDifferenceList.size());
		} else {
			newList = cellDataDifferenceList.subList(startSize, endSize);
		}

		result.put("Rows", newList);
		result.put("Total", cellDataDifferenceList.size());
		return result;
	}
}
